﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using Autofac.Extras.Quartz;
using Autofac.Integration.WebApi;
using Topshelf;
using Topshelf.Autofac;
using Topshelf.LibLog;
using Yks.Notify.Client.Logging;
using Yks.Notify.Client.Services;
using Yks.Notify.Client.Services.Impl;

namespace Yks.Notify.Client
{
    class Program
    {
        public static IContainer Container;

        static void Main(string[] args)
        {
            var builder = new ContainerBuilder();
            builder.RegisterAssemblyTypes(
                    AppDomain.CurrentDomain.GetAssemblies()
                        .Where(x => x.FullName.Contains("Yks.Notify.Client"))
                        .ToArray())
                .AsSelf()
                .AsImplementedInterfaces();
            builder.RegisterModule(new QuartzAutofacFactoryModule());
            builder.RegisterModule(new QuartzAutofacJobsModule(typeof(Program).Assembly));
            builder.Register(t => LogProvider.For<Program>()).AsImplementedInterfaces().SingleInstance();
            builder.Register(t => new GrowlNotifyShower()).AsImplementedInterfaces().SingleInstance();
            builder.RegisterApiControllers();
            Container = builder.Build();
            HostFactory.Run(c =>
            {
                c.UseLibLog();
                c.UseAutofacContainer(Container);
                c.Service<INotifyClient>(s =>
                {
                    s.ConstructUsingAutofacContainer();
                    s.WhenStarted(tc => tc.Start());
                    s.WhenStopped(tc => tc.Stop());
                });
            });
        }
    }
}